package com.ruoyi.bole.integral.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.bole.integral.domain.BoleIntegralApplication;
import com.ruoyi.bole.integral.service.IBoleIntegralApplicationService;
import com.ruoyi.bole.integral.service.IBoleIntegralSeasonService;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bole.integral.mapper.BoleIntegralOrganizationMapper;
import com.ruoyi.bole.integral.domain.BoleIntegralOrganization;
import com.ruoyi.bole.integral.service.IBoleIntegralOrganizationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 组织关系Service业务层处理
 * 
 * @author wujiale
 * @date 2025-07-04
 */
@Service
public class BoleIntegralOrganizationServiceImpl implements IBoleIntegralOrganizationService 
{
    @Autowired
    private BoleIntegralOrganizationMapper boleIntegralOrganizationMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IBoleIntegralApplicationService applicationService;
    @Autowired
    private IBoleIntegralSeasonService seasonService;
    public void a(){
        Long userId = SecurityUtils.getUserId();
        int roleId = 101;
        if(roleId==101){

            //组长权限
            BoleIntegralOrganization boleIntegralOrganization = new BoleIntegralOrganization();
            boleIntegralOrganization.setUserId(userId);
            SysUser sysUser= userService.selectUserById(userId);
            boleIntegralOrganization.setOrgName("用户"+sysUser.getNickName()+"的组");
            boleIntegralOrganization.setLevel(2L);
            boleIntegralOrganization.setStatus("0");
            boleIntegralOrganization.setPoints(0l);
            boleIntegralOrganization.setAllocatedIntegral(0l);
            boleIntegralOrganization.setCreateTime(new Date());
            boleIntegralOrganizationMapper.insertBoleIntegralOrganization(boleIntegralOrganization);
        }else if(roleId==102){
            //团长权限
            //组长权限
            SysUser sysUser= userService.selectUserById(userId);

            BoleIntegralOrganization boleIntegralOrganization = new BoleIntegralOrganization();
            boleIntegralOrganization.setUserId(userId);
            boleIntegralOrganization.setLevel(1L);
            boleIntegralOrganization.setOrgName("用户"+sysUser.getNickName()+"的团");
            boleIntegralOrganization.setStatus("0");
            boleIntegralOrganization.setPoints(99999999L);
            boleIntegralOrganization.setAllocatedIntegral(0l);
            boleIntegralOrganization.setCreateTime(new Date());
        boleIntegralOrganizationMapper.insertBoleIntegralOrganization(boleIntegralOrganization);
        }
    }

    @Override
    public int addMembers(Long orgId, List<Long> userIds) {
        int count = 0;
        for (Long userId :userIds){
            SysUser sysUser= userService.selectUserById(userId);
            BoleIntegralOrganization boleIntegralOrganization = new BoleIntegralOrganization();

            BoleIntegralOrganization temp = boleIntegralOrganizationMapper.getByUserId(userId);
            if(temp!=null){
                //升级
                temp.setLevel(3L);
                temp.setParentId(orgId);
                boleIntegralOrganizationMapper.updateBoleIntegralOrganization(temp);
                continue;
            }
            boleIntegralOrganization.setUserId(userId);
            boleIntegralOrganization.setParentId(orgId);
            boleIntegralOrganization.setLevel(3L);
            boleIntegralOrganization.setOrgName("组员"+sysUser.getNickName());
            boleIntegralOrganization.setCreateTime(new Date());
            boleIntegralOrganizationMapper.insertBoleIntegralOrganization(boleIntegralOrganization);
            count++;
        }
        return count;
    }

    @Override
    @Transactional
    public int batchInsertLeaders(Long orgId, List<Long> userIds) {
        int count = 0;

        for (Long userId :userIds){
            SysUser sysUser= userService.selectUserById(userId);
            BoleIntegralOrganization boleIntegralOrganization = new BoleIntegralOrganization();
            BoleIntegralOrganization temp = boleIntegralOrganizationMapper.getByUserId(userId);
            if(temp!=null){
                //升级
                boleIntegralOrganization.setLevel(2L);
                boleIntegralOrganization.setParentId(orgId);
                boleIntegralOrganizationMapper.updateBoleIntegralOrganization(boleIntegralOrganization);
                continue;
            }

            boleIntegralOrganization.setUserId(userId);
            boleIntegralOrganization.setParentId(orgId);
            boleIntegralOrganization.setLevel(2L);
            boleIntegralOrganization.setOrgName(sysUser.getNickName()+"的组");
            boleIntegralOrganization.setCreateTime(new Date());
             boleIntegralOrganizationMapper.insertBoleIntegralOrganization(boleIntegralOrganization);
             count++;
        }
        return count;
    }

    @Override
    public BoleIntegralOrganization getByuserId() {
        return boleIntegralOrganizationMapper.getByUserId(SecurityUtils.getUserId());
    }

    @Override
    public List<BoleIntegralOrganization> getListAndIsGroupByParentId(Long parentId) {
        List<BoleIntegralOrganization> list =boleIntegralOrganizationMapper.getListAndIsGroupByParentId(parentId);
        for (BoleIntegralOrganization boleIntegralOrganization:list){
            boleIntegralOrganization.setUserName(userService.selectUserById(boleIntegralOrganization.getUserId()).getNickName());
        }

        return list;
    }

    @Override
    public List<BoleIntegralOrganization> getListByParentId(Long parentId) {
        List<BoleIntegralOrganization> list =boleIntegralOrganizationMapper.getListByParentId(parentId);
        for (BoleIntegralOrganization boleIntegralOrganization:list){
            boleIntegralOrganization.setUserName(userService.selectUserById(boleIntegralOrganization.getUserId()).getNickName());
        }

        return list;
    }



    //分配可分配积分
    @Transactional
    @Override
    public int distrubutePointsByOgId(Long orgId, Long points,Long bOrgId) {
        if(boleIntegralOrganizationMapper.reducePointsByoOgId(orgId, points)>0){
            return boleIntegralOrganizationMapper.addPointsByOrgId(bOrgId, points);
        }
        return 0;
    }
    //组长分配给组员或者自己的可用积分
    @Transactional
    @Override
    public int distrubuteAllocatedIntegralByOrgId(Long orgId, Long reducePoints,Long bOrgId,
                                                  Long toUserId,String remark) {
        List<SysRole> roleList = SecurityUtils.getLoginUser().getUser().getRoles();
        Collection<String> roles = roleList.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());
        String temp;
        if(roles.contains("regimental")){
            temp = "团长分配所得："+remark;
        }else{
            temp = "组长分配所得："+remark;
        }
        if(boleIntegralOrganizationMapper.reducePointsByoOgId(orgId, reducePoints)>0){
            BoleIntegralApplication boleIntegralApplication = new BoleIntegralApplication();
            boleIntegralApplication.setUserId(toUserId);
            boleIntegralApplication.setType("project");
            boleIntegralApplication.setScore(reducePoints);
            boleIntegralApplication.setApplyDate(new Date());
            boleIntegralApplication.setDescription(temp);
            boleIntegralApplication.setStatus("1");
            boleIntegralApplication.setSeasonId(seasonService.selectMaxSeason());
            boleIntegralApplication.setIntegralSouce("1");
            if(applicationService.insertBoleIntegralApplication(boleIntegralApplication)>0){
                userService.addScore(toUserId, Math.toIntExact(reducePoints));
                return boleIntegralOrganizationMapper.addAllocatedIntegralByOrgId(bOrgId, reducePoints);
            };
        }
        return 0;
    }

    /**
     * 查询组织关系
     * 
     * @param orgId 组织关系主键
     * @return 组织关系
     */
    @Override
    public BoleIntegralOrganization selectBoleIntegralOrganizationByOrgId(Long orgId)
    {
        return boleIntegralOrganizationMapper.selectBoleIntegralOrganizationByOrgId(orgId);
    }

    /**
     * 查询组织关系列表
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 组织关系
     */
    @Override
    public List<BoleIntegralOrganization> selectBoleIntegralOrganizationList(BoleIntegralOrganization boleIntegralOrganization)
    {
        return boleIntegralOrganizationMapper.selectBoleIntegralOrganizationList(boleIntegralOrganization);
    }

    /**
     * 新增组织关系
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 结果
     */
    @Override
    public int insertBoleIntegralOrganization(BoleIntegralOrganization boleIntegralOrganization)
    {
        boleIntegralOrganization.setCreateTime(DateUtils.getNowDate());
        return boleIntegralOrganizationMapper.insertBoleIntegralOrganization(boleIntegralOrganization);
    }

    /**
     * 修改组织关系
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 结果
     */
    @Override
    public int updateBoleIntegralOrganization(BoleIntegralOrganization boleIntegralOrganization)
    {
        boleIntegralOrganization.setUpdateTime(DateUtils.getNowDate());
        return boleIntegralOrganizationMapper.updateBoleIntegralOrganization(boleIntegralOrganization);
    }

    /**
     * 批量删除组织关系
     * 
     * @param orgIds 需要删除的组织关系主键
     * @return 结果
     */
    @Override
    public int deleteBoleIntegralOrganizationByOrgIds(Long[] orgIds)
    {
        return boleIntegralOrganizationMapper.deleteBoleIntegralOrganizationByOrgIds(orgIds);
    }

    /**
     * 删除组织关系信息
     * 
     * @param orgId 组织关系主键
     * @return 结果
     */
    @Override
    public int deleteBoleIntegralOrganizationByOrgId(Long orgId)
    {
        return boleIntegralOrganizationMapper.deleteBoleIntegralOrganizationByOrgId(orgId);
    }
}
